package com.itany.boot.nmms.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.boot.nmms.entity.ProductType;
import com.itany.boot.nmms.exception.ProductTypeExistException;
import com.itany.boot.nmms.exception.RequestParamsException;
import com.itany.boot.nmms.service.ProductTypeService;
import com.itany.boot.nmms.util.ParamsUtil;
import com.itany.boot.nmms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/productType")
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;

    @RequestMapping("/show")
    public ModelAndView show(String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        if (ParamsUtil.isNull(pageNo)) {
            pageNo = "1";
        }
        if (ParamsUtil.isNull(pageSize)) {
            pageSize = "2";
        }

        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        List<ProductType> productTypes = productTypeService.findAll();
        PageInfo<ProductType> pageInfo = new PageInfo<>(productTypes);
        mav.setViewName("backend/productTypeManager");
        mav.addObject("productTypes", productTypes);
        mav.addObject("pageInfo", pageInfo);
        return mav;
    }

    @ResponseBody
    @RequestMapping("/add")
    public ResponseResult add(String name) {
        ResponseResult result;
        try {
            productTypeService.add(name);
            result = ResponseResult.success();
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (ProductTypeExistException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常:(");

        }

        return result;
    }

    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id) {
        ResponseResult result;
        try {
            productTypeService.modifyStatus(id);
            result = ResponseResult.success();
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常:(");

        }
        return result;

    }

    @ResponseBody
    @RequestMapping("/showModify")
    public ResponseResult showModify(String id) {
        ResponseResult result;
        ProductType productType = productTypeService.findById(id);
        result = ResponseResult.success(productType);

        return result;

    }

    @ResponseBody
    @RequestMapping("/modify")
    public ResponseResult modify(String id, String name) {
        ResponseResult result;
        try {
            productTypeService.modify(id, name);
            result = ResponseResult.success();
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (ProductTypeExistException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常:(");
        }
        return result;
    }

}
